﻿<%@ WebHandler Language="VB" Class="AvaterSaver" %>

Imports System
Imports System.Web
Imports System.IO
Imports System.Web.SessionState

Public Class AvaterSaver : Implements IHttpHandler, IRequiresSessionState
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Session("UID") = 10086
        
        Dim f_big As String = context.Request.Form("big")
        Dim f_middle As String = context.Request.Form("middle")
        Dim f_small As String = context.Request.Form("small")
        
        Dim fs As FileStream
        Dim uid As String = context.Session("UID").ToString()
        Directory.CreateDirectory(context.Server.MapPath("avater/" + uid))
        
        If Not String.IsNullOrEmpty(f_big) Then
            Dim big As Byte() = Convert.FromBase64String(f_big)
            fs = New FileStream(context.Server.MapPath("avater/" + uid + "/big.jpg"), FileMode.Create)
            fs.Write(big, 0, big.Length)
            fs.Close()
        End If
        
        If Not String.IsNullOrEmpty(f_middle) Then
            Dim middle As Byte() = Convert.FromBase64String(f_middle)
            fs = New FileStream(context.Server.MapPath("avater/" + uid + "/middle.jpg"), FileMode.Create)
            fs.Write(middle, 0, middle.Length)
            fs.Close()
        End If
        
        If Not String.IsNullOrEmpty(f_small) Then
            Dim small As Byte() = Convert.FromBase64String(f_small)
            fs = New FileStream(context.Server.MapPath("avater/" + uid + "/small.jpg"), FileMode.Create)
            fs.Write(small, 0, small.Length)
            fs.Close()
        End If
        
        context.Response.ContentType = "text/plain"
        context.Response.Write(1)
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class